﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GroupServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class GroupServices : IGroupServices
    {
        private readonly GroupRepository GroupRepository;
        private WcfSessionStorage _session;
        public GroupServices()
        {
            _session = new WcfSessionStorage();
            GroupRepository = new GroupRepository(_session);
        }

        #region IGroupServices Members

        public List<GroupDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                      string sortType = "ASC")
        {
            var Groups = new List<Group>();
            var Results = new List<GroupDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Groups = GroupRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Groups = GroupRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = GroupRepository.RecordCount;

            Groups.ForEach(x =>
                               {
                                   var groupDto = new GroupDTO();
                                   Mapper.Map(x, groupDto);
                                   Results.Add(groupDto);
                               }
                );

            return Results;
        }

        public bool Save(GroupDTO groupDTO)
        {
            bool res;
            try
            {
                Group group = GroupRepository.GetDataOne(x => x.GroupID == groupDTO.GroupID);
                var user = new User();
                if (group == null)
                {
                    group = new Group();
                    groupDTO.created_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(groupDTO.created_by, user);
                    //groupDTO.modified_by = null;
                }
                else
                {
                    groupDTO.modified_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(groupDTO.modified_by, user);
                }
                Mapper.Map(groupDTO, group);
                GroupRepository.SaveOrUpdate(group);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public bool Delete(GroupDTO groupDto)
        {
            bool res = false;
            try
            {
                var group = new Group();
                Mapper.Map(groupDto, group);
                SubGroupRepository subGroupRepository = new SubGroupRepository(_session);
                var subgroupList =   subGroupRepository.GetDataList(x => x.Group.GroupID == group.GroupID);
                if (subgroupList.Count > 0) return res;
                GroupRepository.Delete(group);
                res = true;
            }
            catch (Exception ex)
            {
                res = false;
            }

            return res;
        }


        public List<GroupDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                out long recordCount)
        {
            var groups = new List<Group>();
            var Results = new List<GroupDTO>();
            groups = GroupRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = GroupRepository.RecordCount;
            groups.ForEach(x =>
                               {
                                   var groupDto = new GroupDTO();
                                   Mapper.Map(x, groupDto);
                                   Results.Add(groupDto);
                               }
                );

            return Results;
        }

        public List<GroupDTO> GetAll()
        {
            var groups = new List<Group>();
            var Results = new List<GroupDTO>();
            groups = GroupRepository.FetchAll().ToList();
            groups.ForEach(x =>
                               {
                                   var groupDto = new GroupDTO();
                                   Mapper.Map(x, groupDto);
                                   Results.Add(groupDto);
                               }
                );

            return Results;
        }

        public bool SendEmail(GroupDTO groupDto, String DestinationFolder)
        {
            var mailServices = new MailServices();
            var Group = GroupRepository.GetDataOne(x => x.GroupID == groupDto.GroupID);
            groupDto = Mapper.Map(Group, groupDto);
            return mailServices.SendEmail(groupDto,DestinationFolder);
        }

        #endregion
    }
}